home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / UI / UI_TEXT.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  8KB  |  297 lines

  1. /****************************************************************
  2.  
  3.     ui_text.c       Text handling routines for
  4.             The Bywater Graphical User Interface
  5.  
  6.             Copyright (c) 1991, Ted A. Campbell
  7.  
  8.             Bywater Software
  9.             P. O. Box 4023
  10.             Duke Station
  11.             Durham, NC  27706
  12.  
  13.             email: tcamp@hercules.acpub.duke.edu
  14.  
  15.     Copyright and Permissions Information:
  16.  
  17.     All U.S. and international copyrights are claimed by the
  18.     author. The author grants permission to use this code
  19.     and software based on it under the following conditions:
  20.     (a) in general, the code and software based upon it may be
  21.     used by individuals and by non-profit organizations; (b) it
  22.     may also be utilized by governmental agencies in any country,
  23.     with the exception of military agencies; (c) the code and/or
  24.     software based upon it may not be sold for a profit without
  25.     an explicit and specific permission from the author, except
  26.     that a minimal fee may be charged for media on which it is
  27.     copied, and for copying and handling; (d) the code must be
  28.     distributed in the form in which it has been released by the
  29.     author; and (e) the code and software based upon it may not
  30.     be used for illegal activities.
  31.  
  32. ****************************************************************/
  33.  
  34. #include "stdio.h"
  35. #include "ctype.h"
  36. #include "string.h"
  37. #include "gr.h"
  38. #include "bw.h"
  39. #include "ui.h"
  40.  
  41. #define MAXBUF  128
  42. #define PIXEL_SPACES    3
  43. #define SAFETY_MARGIN   1
  44.  
  45. /* #define GRDIRECT */               /* use gr_text function from ui_text */
  46.                 /* else use ui_str from ui_text */
  47.  
  48. ui_str( x1, y1, x2, background, foreground, buffer )
  49.    int x1, y1, x2, background, foreground;
  50.    char *buffer;
  51.    {
  52.    char buf[ MAXBUF ];
  53.    int y, c;
  54.  
  55.    /* Write to buffer until overflow occurs */
  56.  
  57.    c = 0;
  58.    y = FALSE;
  59.    buf[ 0 ] = 0;
  60.    while( y == FALSE )
  61.       {
  62.       buf[ c ] = buffer[ c ];
  63.       ++c;
  64.       buf[ c ] = 0;
  65.  
  66.       if ( c >= MAXBUF )
  67.      {
  68.      y = TRUE;
  69.      }
  70.       else if ( gr_strlen( buf ) > ( x2 - x1 ) )
  71.      {
  72.      y = TRUE;
  73.      }
  74.       else if ( buffer[ c ] == 0 )
  75.      {
  76.      y = TRUE;
  77.      }
  78.       }
  79.  
  80.    /* Now reverse until last whitespace */
  81.  
  82.    --c;
  83.    if ( gr_strlen( buf ) > ( x2 - x1 ))
  84.       {
  85.       while( isspace( buf[ c ] ) == FALSE )
  86.      {
  87.      buf[ c ] = 0;
  88.      --c;
  89.      }
  90.       }
  91.  
  92.    if ( isspace( buf[ c ] ) != FALSE )
  93.       {
  94.       buf[ c ] = 0;
  95.       }
  96.  
  97.    /* Display the string in the appropriate colors */
  98.  
  99.    gr_text( ui_screen, x1, y1, buf, foreground, background );
  100.  
  101.    }
  102.  
  103. ui_text( x1, y1, x2, y2, maxlines, background, foreground, main_buffer )
  104.    int x1, y1, x2, y2;          /* Coordinates of box */
  105.    int maxlines;                /* Maximum lines to be used */
  106.    int background;              /* Background color */
  107.    int foreground;              /* Foreground color */
  108.    char *main_buffer;           /* Text buffer */
  109.    {
  110.    int l, c, y, main_pos, word_pos, lcount, line_pos, newline;
  111.    int line_start, line_end, word_start, word_end;
  112.    static char line_buffer[ 128 ];
  113.    static char word_buffer[ 64 ];
  114. #ifdef  OLD_DEBUG
  115.    int X;
  116.    static char Dbuffer[ BW_EBUFSIZE ];
  117. #endif
  118.  
  119.    if ( ui_ready == FALSE )
  120.       {
  121.       bw_error( "The user interface is not initialized." );
  122.       return;
  123.       }
  124.  
  125.    /* set initial values */
  126.  
  127.    l = y2 - ( ui_grwind->fysize + PIXEL_SPACES );       /* l = location on y axis */
  128.    y = TRUE;                                    /* continue loop */
  129.    main_pos = 0;                                /* position in main buffer */
  130.    lcount = 0;                                  /* count of lines */
  131.    line_start = line_end = 0;
  132.  
  133.    /* loop for each word detected */
  134.  
  135.    while( y == TRUE )
  136.       {
  137.       newline = FALSE;
  138.  
  139.       /* if l extends below the box, abort */
  140.  
  141.       if ( l < y1 )
  142.      {
  143.      return;
  144.      }
  145.  
  146.       /* if line count exceeds max allowed, abort */
  147.  
  148.       if ( lcount >= maxlines )
  149.      {
  150.      return;
  151.      }
  152.  
  153.       /* Get one word (up to white space or end) in buffer */
  154.  
  155.       word_start = word_end = main_pos;
  156.       word_pos = 0;
  157.       word_buffer[ 0 ] = 0;
  158.       while( ( isspace( main_buffer[ main_pos ] ) == FALSE )
  159.      && ( main_buffer[ main_pos ] != 0 ))
  160.      {
  161.      word_buffer[ word_pos ] = main_buffer[ main_pos ];
  162.      ++word_end;
  163.      ++main_pos;
  164.      ++word_pos;
  165.      word_buffer[ word_pos ] = 0;                 /* terminate word with 0 */
  166.      }
  167.  
  168.       /* Advance past any whitespace */
  169.  
  170.       while ( ( isspace( main_buffer[ main_pos ] ) != FALSE )
  171.      && ( main_buffer[ main_pos ] != 0 ))
  172.      {
  173.      if ( main_buffer[ main_pos ] == '\n' )
  174.         {
  175.         newline = TRUE;
  176.         word_buffer[ word_pos ] = 0;
  177.         }
  178.      else
  179.         {
  180.         word_buffer[ word_pos ] = main_buffer[ main_pos ];
  181.         }
  182.      ++main_pos;
  183.      ++word_pos;
  184.      ++word_end;
  185.      word_buffer[ word_pos ] = 0;                 /* set end of buffer to 0 */
  186.      }
  187.  
  188. #ifdef  OLD_DEBUG
  189.       if ( gr_strlen( word_buffer ) > 100 )
  190.      {
  191.      sprintf( Dbuffer, "String element [%s]: may contain CR/LF",
  192.          word_buffer );
  193.      bw_debug( Dbuffer );
  194.      }
  195. #endif
  196.  
  197.       /* Get the current line into the buffer */
  198.  
  199.       line_pos = 0;
  200.       line_buffer[ 0 ] = 0;
  201.       for ( c = line_start; c < line_end; ++c )
  202.      {
  203.      line_buffer[ line_pos ] = main_buffer[ c ];
  204.      ++line_pos;
  205.      line_buffer[ line_pos ] = 0;                   /* terminate with 0 */
  206.      }
  207.  
  208. #ifdef  OLD_DEBUG
  209.       sprintf( Dbuffer, "line_buffer [%s], word_buffer [%s]", line_buffer, word_buffer );
  210.       bw_debug( Dbuffer );
  211. #endif
  212.  
  213.       /* Now, what do we do with the word we have detected? */
  214.       /* 1. Check to see if new word will extend past limits */
  215.       /*    if so, output 'line_buffer' only, and make word the */
  216.       /*    new line_buffer */
  217.  
  218.       if ( ( x1 + gr_strlen( line_buffer ) + gr_strlen( word_buffer ) ) >= ( x2 - SAFETY_MARGIN ))
  219.      {
  220. #ifdef  OLD_DEBUG
  221.      sprintf( Dbuffer, "Flush line: x1 = %d, line = %d, word = %d",
  222.         x1, gr_strlen( line_buffer ), gr_strlen( word_buffer ) );
  223.      bw_debug( Dbuffer );
  224.      sprintf( Dbuffer, "Flush line: x1 + word + line = %d, x2 = %d",
  225.         x1 + gr_strlen( line_buffer ) + gr_strlen( word_buffer ), x2 );
  226.      bw_debug( Dbuffer );
  227.      sprintf( Dbuffer, "Flush line: strlen( line ) = %d",
  228.         strlen( line_buffer ) );
  229.      bw_debug( Dbuffer );
  230. #endif
  231. #ifdef  GRDIRECT
  232.      gr_text( ui_screen, x1, l, line_buffer, foreground,
  233.         background );
  234. #else
  235.      ui_str( x1, l, x2, background, foreground, line_buffer );
  236. #endif
  237.      ++lcount;
  238.      l -= ( ui_grwind->fysize + PIXEL_SPACES );
  239.  
  240.      /* now make the current word the new line */
  241.  
  242.      line_start = word_start;
  243.      line_end   = word_end;
  244.  
  245.      }
  246.  
  247.       /* 2. check to see if this is the end of the main buffer */
  248.       /*    if so, flush and return */
  249.  
  250.       else if ( main_buffer[ main_pos ] == 0 )
  251.      {
  252.      strcat( line_buffer, word_buffer );
  253. #ifdef  GRDIRECT
  254.      gr_text( ui_screen, x1, l, line_buffer,
  255.         foreground, background );
  256. #else
  257.      ui_str( x1, l, x2, background, foreground, line_buffer );
  258. #endif
  259.      return;
  260.      }
  261.  
  262.       /* 3. check to see if a newline has been encountered; if so */
  263.       /*    output the current line */
  264.  
  265.       else if ( newline == TRUE )
  266.      {
  267.  
  268. #ifdef  OLD_DEBUG
  269.      sprintf( bw_ebuf, "Got a newline after %s|%s",
  270.         line_buffer, word_buffer );
  271.      bw_debug( bw_ebuf );
  272. #endif
  273.  
  274.      strcat( line_buffer, word_buffer );
  275. #ifdef  GRDIRECT
  276.      gr_text( ui_screen, x1, l, line_buffer,
  277.         foreground, background );
  278. #else
  279.      ui_str( x1, l, x2, background, foreground, line_buffer );
  280. #endif
  281.      ++lcount;
  282.      l -= ( ui_grwind->fysize + PIXEL_SPACES );
  283.      line_start = line_end = main_pos;
  284.      }
  285.  
  286.       /* 4. else set end of current line to word_end and loop through */
  287.       /*    again to get another word */
  288.  
  289.       else
  290.      {
  291.      line_end = word_end;
  292.      }
  293.       }
  294.    }
  295.  
  296.  
  297.